using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WhsFacebook.Whs.Contracts;
using WhsFacebook.Whs.ErrorReporting;
using WhsFacebook.Whs.Service.ErrorReporting;

namespace WhsFacebook.Whs.Service
{
    partial class SingletonService
    {
        public bool IsErrorReportsSubmissionEnabled()
        {
            try
            {
                return Properties.Settings.Default.EnableErrorReportsSubmission;
            }
            catch (Exception e)
            {
                _log.Error("IsErrorReportsSubmissionEnabled() failed", e);

                throw;
            }
        }

        public void SetErrorReportsSubmissionEnabled(bool enable)
        {
            SetErrorReportsSubmissionEnabled(enable, true);
        }

        public void SetErrorReportsSubmissionEnabled(bool enable, bool save)
        {
            try
            {
                Properties.Settings.Default.EnableErrorReportsSubmission = enable;

                if (save)
                    Properties.Settings.Default.Save();

                // enable or disable
                if (enable)
                    ErrorReportingSingleton.Instance.StartSubmittingErrors();
                else
                    ErrorReportingSingleton.Instance.StopSubmittingErrors(true);
            }
            catch (Exception e)
            {
                _log.Error("SetErrorReportsSubmissionEnabled() failed", e);

                throw;
            }
        }

        public ErrorReportingStatus GetErrorReportingStatus()
        {
            try
            {
                var eri = ErrorReportingStats.Instance;

                return new ErrorReportingStatus()
                {
                    IsSubmitting = eri.IsSubmitting,
                    Progress = eri.SubmittingProgress,
                    Submitted = eri.SubmittedErrors,
                    Unsubmitted = eri.UnsubmittedErrors
                };
            }
            catch (Exception e)
            {
                _log.Error("GetErrorReportingStatus() failed", e);

                throw;
            }
        }
    }
}
